package com.vains.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.vains.dto.request.UserRoleReq;
import com.vains.entity.OauthUser;
import com.vains.entity.OauthUserRole;
import com.vains.mapper.OauthUserMapper;
import com.vains.service.IOauthUserRoleService;
import com.vains.service.IOauthUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 用户表 服务实现类
 * </p>
 *
 * @author vains
 * @since 2020-06-08
 */
@Slf4j
@Service
public class OauthUserServiceImpl extends ServiceImpl<OauthUserMapper, OauthUser> implements IOauthUserService {

    private final IOauthUserRoleService oauthUserRoleService;

    public OauthUserServiceImpl(IOauthUserRoleService oauthUserRoleService) {
        this.oauthUserRoleService = oauthUserRoleService;
    }

    @Override
    public List<OauthUser> getAllUsers(Page<OauthUser> page) {
        QueryWrapper<OauthUser> wrapper = new QueryWrapper<>();
        // 只查询启用的用户
        wrapper.eq("user_enabled", true);
        return baseMapper.selectPage(page, wrapper).getRecords();
    }

    @Override
    public List<Integer> getRoleByUserId(Integer userId) {
        return baseMapper.getRoleByUserId(userId);
    }

    @Override
    @Transactional
    public void resetUserRole(UserRoleReq userRoleReq) {
        Assert.notNull(userRoleReq, "入参不能为空!");
        // 先删除用户和角色对应的关系
        Integer userId = userRoleReq.getUserId();
        Assert.notNull(userId, "用户ID不能为空!");
        // 判断数据库中是否存在
        QueryWrapper<OauthUserRole> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId);
        log.info("查询用户 {} 对应的角色ID...", userId);
        List<OauthUserRole> oauthUserRoles = oauthUserRoleService.list(queryWrapper);
        if (!CollectionUtils.isEmpty(oauthUserRoles)) {
            log.info("用户之前已有角色, 删除原有记录...");
            // 如果数据库中存在就删除记录
            UpdateWrapper<OauthUserRole> updateWrapper = new UpdateWrapper<>();
            updateWrapper.eq("user_id", userId);
            oauthUserRoleService.remove(updateWrapper);
        }
        // 不存在就直接添加
        List<Integer> userRoleId = userRoleReq.getUserRoleId();
        if (CollectionUtils.isEmpty(userRoleId)) {
            log.info("用户 {} 修改后的角色是空的,跳过添加过程.", userId);
            // 如果角色ID是空的,则跳过后边的添加过程
            return;
        }
        // 待添加集合
        List<OauthUserRole> userRoles = new ArrayList<>();
        // 如果角色ID不是空的,则添加记录
        for (Integer roleId : userRoleId) {
            if (roleId == null) {
                // 如果角色ID是空的,则跳过
                continue;
            }
            OauthUserRole userRole = new OauthUserRole();
            userRole.setRoleId(roleId);
            userRole.setUserId(userId);
            userRoles.add(userRole);
        }
        if (!CollectionUtils.isEmpty(userRoles)) {
            log.info("开始赋予用户 {} 角色...", userId);
            // 批量添加
            oauthUserRoleService.saveBatch(userRoles);
        }
    }
}
